<?php

namespace app\api\controller\admin\setting;

use app\api\common\controller\BaseApi;
use app\api\services\admin\setting\AccountServices;
use app\Request;
use think\App;

class AccountController extends BaseApi
{

    private AccountServices $services;

    /**
     * 构造函数
     *
     * @param \app\Request                                    $request
     * @param \think\App                                      $app
     * @param \app\api\services\admin\setting\AccountServices $services
     */
    public function __construct(Request $request, App $app, AccountServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    public function info(Request $request, $id): \think\response\Json
    {
        $where   = $request->getParams([['id']]);
        $rule    = ['id' => 'require'];
        $message = [
            'id.require' => '必须参数ID不能为空',
        ];
        $this->validate($where, $rule, $message);
        $ret = $this->services->getInfo($where);
        return ret_value(0, "ok", $ret['list'], 200);
    }

    /**
     * 保存更新资源
     *
     * @param \app\Request $request
     * @param              $id
     *
     * @return \think\response\Json
     */
    public function update(Request $request, $id): \think\response\Json
    {
        $where   = $request->getParams([['id']]);
        $data    = $request->getParams([
            ['real_name'],
            ['email'],
            ['mobile_phone'],
        ]);
        $rule    = ['id' => 'require'];
        $message = [
            'id.require' => '必须参数ID不能为空',
        ];
        $this->validate($where, $rule, $message);
        $rule    = ['real_name' => 'require'];
        $message = [
            'real_name.require' => '昵称不能为空',
        ];
        $this->validate($data, $rule, $message);
        $ret = $this->services->update($where, $data);
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");
    }

    public function password(Request $request): \think\response\Json
    {
        $where   = $request->getParams([['id']]);
        $data    = $request->getParams([
            ['old_password'],
            ['password'],
            ['password_confirm'],
        ]);
        $rule    = ['id' => 'require'];
        $message = [
            'id.require' => '必须参数ID不能为空',
        ];
        $this->validate($where, $rule, $message);
        $rule    = ['old_password' => 'require', 'password' => 'require', 'password_confirm' => 'require'];
        $message = [
            'real_name.require' => '昵称不能为空',
        ];
        $this->validate($data, $rule, $message);
        $ret = $this->services->password($where, $data);

        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");
    }
}
